package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.qiaotai.Personnel;
import com.tongzhan.entity.qiaotai.TownInfo;
import com.tongzhan.param.PersonnelParam;
import com.tongzhan.utils.RegexUtil;
import com.tongzhan.utils.SnowFlakeUtils;
import com.tongzhan.vo.PersonnelTownVo;
import com.tongzhan.vo.PersonnelTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员信息
 * @author htmic
 * @date 2020/7/31
 */
@RequestMapping("/personnel")
@RestController
public class PersonnelController extends BaseController {

    /**
     * 城镇信息列表
     * @return
     */
    @PostMapping("/townList")
    public Return townList() {
        List<TownInfo> list = personnelService.getTownPageList();
        return Return.getSuccessResult(list);
    }

    /**
     * 新增城镇信息
     * @param name
     * @return
     */
    @PostMapping("/addTown")
    public Return addTown(String name) {
        TownInfo param = new TownInfo();
        param.setId(new SnowFlakeUtils(3, 3).nextId()+"");
        param.setName(name);
        personnelService.addTown(param);
        return Return.getSuccessResult();
    }


    /**
     * 人员信息类型列表
     * @return
     */
    @PostMapping("/personnelTypeList")
    public Return personnelTypeList() {
        List<PersonnelTypeVo> list = personnelService.getTypeList();
        return Return.getSuccessResult(list);
    }

    /**
     * 人员信息城镇列表
     * @return
     */
    @PostMapping("/personnelTownList")
    public Return personnelTownList(Integer typeId) {
        List<PersonnelTownVo> list = personnelService.getTownList(typeId);
        return Return.getSuccessResult(list);
    }

    /**
     * 人员信息列表
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/personnelList")
    public Return personnelList(Integer page, Integer size,String townTypeId, Integer typeId) {
        page = page == null ? 0 : page - 1;
        Page<Personnel> list = personnelService.getPersonnelList(page, size,townTypeId, typeId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }

    /**
     * 人员信息详情
     * @param id
     * @return
     */
    @PostMapping("/personnelDetail")
    public Return getDetail(String id) {
        if (id == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        Personnel vo = personnelService.getDetail(id);
        return Return.getSuccessResult(vo);
    }

    /**
     * 添加
     * @param param
     * @return
     */
    @PostMapping("/addPersonnel")
    public Return add(PersonnelParam param) {
        if (param == null || param.getIdcard() == null || param.getName() == null
        ||param.getTypeId()==null||param.getTownTypeId()==null) {
            return Return.getErrorResult("参数为空");
        }
        if (param.getName().length() > 50) {
            return Return.getErrorResult("名字过长");
        }
        if (!RegexUtil.checkIdCardLax(param.getIdcard())) {
            return Return.getErrorResult("无效身份证号");
        }

        Personnel entity = new Personnel();
        BeanUtils.copyProperties(param, entity);
        entity.setId(new SnowFlakeUtils(3, 3).nextId()+"");
        entity.setAddUserId(param.getUserId());
        long l = personnelService.add(entity);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }
}
